﻿using System.Collections;
using System.Collections.Generic;

namespace CalmNine.Framework
{
    /// <summary>
    /// 字典扩展类
    /// </summary>
    public static class DictionaryExtension
    {
        /// <summary>
        /// 将数据放入字典
        /// 字典为空时会创建一个字典
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dic"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="isCover">是否覆盖原有的数据</param>
        public static void Put<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue value, bool isCover = true)
        {
            if (dic == null)
            {
                dic = new Dictionary<TKey, TValue>();
            }
            if (dic.ContainsKey(key))
            {
                if (isCover)
                {
                    dic[key] = value;
                }
            }
            else
            {
                dic.Add(key, value);
            }
        }
    }
}